<?php
class PFC_AjaxFileUpload extends PFC_Base {
	
	/**
	 * Called (on server) after file has been uploaded and validated.
	 * @var callback
	 */
	protected $onSuccessServer		= NULL;
	
	/**
	 * Called (on client) after file has been uploaded and validated. 
	 * @var string JS function name
	 */
	protected $onSuccessClient		= NULL;
	
	/**
	 * Called on errors 
	 * @var string JS function name
	 */
	protected $onErrorClient 		= NULL;
	
	/**
	 * JS function called to produce any extra data you want to send along with file
	 * @var string JS function name
	 */
	protected $extraDataProducer	= NULL;
	
	/**
	 * Whether or not uploading process should start automatically after user choose a file 
	 * @var bool
	 */
	protected $autoUpload 			= TRUE;
	
	/**
	 * @return the $extraDataProducer
	 */
	public function getExtraDataProducer() {
		return $this->extraDataProducer;
	}

	/**
	 * @param string $extraDataProducer
	 */
	public function setExtraDataProducer($extraDataProducer) {
		$this->extraDataProducer = $extraDataProducer;
		return $this;
	}

	/**
	 * @return the $onSuccessServer
	 */
	public function getOnSuccessServer() {
		return $this->onSuccessServer;
	}

	/**
	 * @param callback $onSuccessServer
	 */
	public function setOnSuccessServer($onSuccessServer) {
		$this->onSuccessServer = $onSuccessServer;
		return $this;
	}

	/**
	 * @return the $onSuccessClient
	 */
	public function getOnSuccessClient() {
		return $this->onSuccessClient;
	}

	/**
	 * @param string $onSuccessClient
	 */
	public function setOnSuccessClient($onSuccessClient) {
		$this->onSuccessClient = $onSuccessClient;
		return $this;
	}

	/**
	 * @return the $onErrorClient
	 */
	public function getOnErrorClient() {
		return $this->onErrorClient;
	}

	/**
	 * @param string $onErrorClient
	 */
	public function setOnErrorClient($onErrorClient) {
		$this->onErrorClient = $onErrorClient;
		return $this;
	}

	/**
	 * @return the $autoUpload
	 */
	public function getAutoUpload() {
		return $this->autoUpload;
	}

	/**
	 * @param boolean $autoUpload
	 */
	public function setAutoUpload($autoUpload) {
		$this->autoUpload = $autoUpload;
		return $this;
	}

	function __construct( $name, $onSuccessServer) {
		$this->name       		= $name;
		$this->onSuccessServer	= $onSuccessServer;
		parent::__construct();
	}
	

	function buildContent() {
		Page::jsBatchAdd( array(Locator::urlThisLib() . "/js/PFC_AjaxFileUpload.js") );
		$idFormForUpload 	= "upload_form_" . $this->htmlId();
		$idIframeForUpload 	= "upload_iframe_" . $this->htmlId();
		
		$hiddenUploadForm  = '<form style="height:0px; width:0px;" target="' . $idIframeForUpload . '"'; 
		$hiddenUploadForm .= 'id="' . $idFormForUpload . '" method="post" enctype="multipart/form-data"'; 
		$hiddenUploadForm .= 'action="' . PersistentForm::getBackendUrl() . '/?form-name=' . $this->form->getName() . '&service=ajax-file-upload&control-name=' . $this->name . '"></form>';
		
		$hiddenUploadIframe  = '<iframe src="' . PersistentForm::getBackendUrl() . '/?service=blank-page" style="height:0px; width:0px;display:none;" id="' . $idIframeForUpload . '" name="' . $idIframeForUpload . '"></iframe>'; 
		
		$hiddenFormAndIframe = $hiddenUploadForm . $hiddenUploadIframe;
		//echo htmlspecialchars($hiddenUploadForm); 
		ob_start();
		?>
		<input 
			type="file" 
			id="<?php echo $this->htmlId() ?>" 
			name="<?php echo $this->htmlName() ?>" 
			<?php echo $this->buildAttrsAsString( $this->attrs, array('type', 'id', 'name') ) ?>
			<?php echo ($this->autoUpload ? " onchange=\"pd.pform.getControl('{$this->getForm()->getName()}', '{$this->name}').startUpload()\"" : '') ?> 
		/>
		<script>
		var control = new pd.pform.PFC_AjaxFileUpload(
				pd.pform.get("<?php echo $this->form->getName() ?>"),
				'<?php echo $this->name ?>', 
				'<?php echo $this->htmlName() ?>',
				'<?php echo $this->htmlId() ?>',
				'<?php echo $idFormForUpload ?>',
				'<?php echo $this->onSuccessClient ?>', 
				'<?php echo $this->onErrorClient ?>', 
				'<?php echo $this->extraDataProducer ?>', 
				<?php echo ($this->autoUpload ? 'true' : 'false') ?>
		);
		pd.pform.get("<?php echo $this->form->getName() ?>").addControl('<?php echo $this->name ?>', control);
		$(function(){
			var idForm 			= "<?php echo $this->form->getName() ?>";
			var idControl		= "<?php echo $this->htmlId() ?>";
			var idFormForUpload = "<?php echo $idFormForUpload ?>";
			var controlParent   = $("#" + idControl).parent(); 
			var controlClone    = $("#" + idControl).clone();

			controlClone.attr("id", "<?php $this->htmlId() ?>-clone");
			controlClone.css("z-index", "-1");
			controlClone.css("visibility", "hidden");
			
			var controlOffset   = $("#" + idControl).offset();	
			
			// add hidden form and iframe 
			$("#" + idForm).parent().append('<?php echo $hiddenFormAndIframe ?>');

			// add control into form for upload
			$("#" + idFormForUpload).append( $("#" + idControl) );
			
			// hook to initial place
			
			controlParent.append(controlClone);
			$("#" + idControl).offset({top:controlOffset.top, left:controlOffset.left});
			
		});
		</script>
		<?php
		return ob_get_clean();
	}

	function setValueOnSubmit() {
		$this->value = @Request::file( $this->form->getName(), $this->name );
	}
}
?>